<template>
  <div class="bindWarehouse-container app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.pre_db_para_is_online"
        placeholder="绑定状态"
        clearable
        class="filter-item select-item"
        @clear="handleFilter"
      >
        <el-option
          v-for="item in statusOptions"
          :key="item.key"
          :label="item.name"
          :value="item.key"
        />
      </el-select>
      <el-input
        v-model="listQuery.agent_name"
        placeholder="仓库名称"
        clearable
        class="filter-item input-item"
        style="width: 200px"
        @clear="handleFilter"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <div class="page-content">
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          label="ID"
          prop="id"
          align="center"
          width="120"
          :class-name="getSortClass('id')"
        >
          <template slot-scope="{ row }">
            <span>{{ row.id_public_agent_info }}</span>
          </template>
        </el-table-column>
        <el-table-column label="仓库名称" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.agent_name }}-{{ row.is_online }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系电话" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.agent_phone || "-" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="门店地址" width="340" align="center">
          <template slot-scope="{ row }">
            <span
              >{{ row.agent_province }} {{ row.agent_city }}
              {{ row.agent_country }} {{ row.agent_addr }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          label="绑定状态"
          align="center"
          width="100"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row }">
            <el-switch
              v-model="row.is_online"
              inactive-color="#ff4949"
              :active-value="0"
              :inactive-value="1"
              @change="handleBind(row)"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="currentPage"
      :limit.sync="listQuery.limit"
      @pagination="getPageList"
    />
  </div>
</template>

<script>
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { warehouseList } from "@/api/warehouse";

export default {
  name: "BindWarehouse",
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      currentPage: 0,
      listQuery: {
        agent_type_4_use: 10,
        pre_db_spec_buyer_id: null,
        invoke_method: "public_page_4m_supply_list_2_reg",
        pre_db_para_begin_idx: 0,
        pre_db_para_length_4_list: 10,
        agent_name: null,
        pre_db_para_is_online: null,
      },
      bindQuery: {
        invoke_method: "public_page_4m_reg_supply_4_retail",
        seller_id: null,
        buyer_id: null,
        is_online: null,
      },
      statusOptions: [
        { key: 0, name: "已绑定" },
        { key: 1, name: "未绑定" },
      ],
    };
  },
  created() {},
  mounted() {
    if (this.$route.params) {
      this.listQuery.pre_db_spec_buyer_id = this.$route.params.id;
    }
    this.getList();
  },
  methods: {
    /*
     * 获取页面数据
     * */
    getList() {
      this.listLoading = true;
      let params;
      if (
        this.listQuery.pre_db_para_is_online === "0" ||
        !this.listQuery.pre_db_para_is_online
      ) {
        params = {
          agent_type_4_use: 10,
          pre_db_spec_buyer_id: this.listQuery.pre_db_spec_buyer_id,
          invoke_method: "public_page_4m_supply_list_2_reg",
          pre_db_para_begin_idx:this.listQuery.pre_db_para_begin_idx,
          pre_db_para_length_4_list: this.listQuery.pre_db_para_length_4_list,
          agent_name: this.listQuery.agent_name,
          "public_agent_support_4_another.is_online": this.listQuery.pre_db_para_is_online,
          "public_agent_info.is_online": 0
        };
      } else {
        params = {
          agent_type_4_use: 10,
          pre_db_spec_buyer_id: this.listQuery.pre_db_spec_buyer_id,
          invoke_method: "public_page_4m_supply_list_2_reg",
          pre_db_para_begin_idx:this.listQuery.pre_db_para_begin_idx,
          pre_db_para_length_4_list: this.listQuery.pre_db_para_length_4_list,
          agent_name: this.listQuery.agent_name,
          "public_agent_support_4_another.id_public_agent_support_4_another_is_null": true,
          "public_agent_info.is_online": 0
        };
      }
      warehouseList(this.Qs.stringify(params)).then((response) => {
        this.list = response.page_content;
        this.total = response.result_num;

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 0.5 * 1000);
      });
    },
      /*
    * 获取页面数据
    * */
    // getList() {
    //   this.listLoading = true
    //   warehouseList(this.Qs.stringify({
    //     agent_type_4_use: 10,
    //     pre_db_spec_buyer_id: this.listQuery.pre_db_spec_buyer_id,
    //     invoke_method: 'public_page_4m_supply_list_2_reg',
    //     pre_db_para_begin_idx: 0,
    //     pre_db_para_length_4_list: 10,
    //     agent_name: this.listQuery.agent_name,
    //     'public_agent_support_4_another.is_online': this.listQuery.pre_db_para_is_online
    //   })).then(response => {
    //     this.list = response.page_content
    //     this.total = response.result_num
    //     console.log(this.list);
    //     // Just to simulate the time of the request
    //     setTimeout(() => {
    //       this.listLoading = false
    //     }, 0.5 * 1000)
    //   })
    // },
    /*
     * 点击分页进行查询
     * */
    getPageList(val) {
      this.listQuery.pre_db_para_begin_idx =
        (val.page - 1) * this.listQuery.pre_db_para_length_4_list;
      this.getList();
    },
    /*
     * 查询数据
     * */
    handleFilter() {
      this.listQuery.pre_db_para_begin_idx = 0;
      this.getList();
    },
    getSortClass: function (key) {
      const sort = this.listQuery.sort;
      return sort === `+${key}` ? "ascending" : "descending";
    },

    /*
     * 绑定操作
     * */
    handleBind(item) {
      console.log(item);
      if (item.is_online === 1) {
        this.$confirm("您确定要解除与该供应商的绑定吗?", "提示", {
          type: "warning",
        })
          .then(() => {
            this.bindQuery.is_online = item.is_online;
            this.bindQuery.seller_id = item.id_public_agent_info;
            this.bindQuery.buyer_id = this.$route.query.agent_id;
            warehouseList(this.Qs.stringify(this.bindQuery)).then(
              (response) => {
                this.$notify({
                  title: "Success",
                  message: "操作成功",
                  type: "success",
                  duration: 2000,
                });
                this.getList();
              }
            );
          })
          .catch(() => {
            this.getList();
          });
      } else {
        this.bindQuery.is_online = item.is_online;
        this.bindQuery.seller_id = item.id_public_agent_info;
        this.bindQuery.buyer_id = this.$route.query.agent_id;
        warehouseList(this.Qs.stringify(this.bindQuery)).then((response) => {
          this.$notify({
            title: "Success",
            message: "操作成功",
            type: "success",
            duration: 2000,
          });
          if (
            this.total - 1 ==
            (this.currentPage - 1) * this.listQuery.pre_db_para_length_4_list
          ) {
            if (this.currentPage - 2 > 0 || this.currentPage - 2 == 0) {
              this.listQuery.pre_db_para_begin_idx =
                (this.currentPage - 2) *
                this.listQuery.pre_db_para_length_4_list;
            }
          }
          this.getList();
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
